package com.snts.synchronization.listener;

import java.io.File;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.InitializingBean;
import com.snts.synchronization.collector.Collector;

public abstract class BasePollingListener implements Listener, Runnable {

	protected Collector collector;

	private Timer timer;
	private TimerTask timerTask;

	private long pollInterval = 60 * 1000;
	
	@Override
	public void setCollector(Collector collector) {
		this.collector = collector;
	}
	
	public long getPollInterval() {
		return pollInterval;
	}

	public void setPollInterval(long pollInterval) {
		this.pollInterval = pollInterval;
	}

	public void start(){
		if(timer == null){
			timer = new Timer(true);
			timerTask = new DelegatingTimerTask(this);
			timer.schedule(timerTask, new Date(), pollInterval);
		} else {
			throw new IllegalStateException("Polling listener have been already started");
		}
	}
	
	@Override
	public void run() {
		poll();
	}

	
	public abstract void poll();
	
	public static void main(String[] args){
		File file = new File(".");
		System.out.println(file.getAbsolutePath());
	}

}
